package reco.db.validation

import reco.db.*
import reco.db.field.*


scenario "having a between constraint", {
    given "a constraint for checking a integer is between 1 and 10", {
        constraint = new BetweenConstraint<Integer>(1, 10)
    }
}

scenario "error arguments are 1 and 10", {
    when "error arguments are required", {
        errorArguments = constraint.errorArguments
    }
    then "it should be not null", {
        errorArguments.shouldNotBe null 
    }
    and "it should be defined as [min, max]", {
        expectedArguments = [1, 10] as Object[]
        errorArguments.shouldBe expectedArguments
    }
}

scenario "null value is checked", {
    when "a null integer is checked", {
        isValid = constraint.validate(null)
    }
    then "it should be considered valid", {
        isValid.shouldBe true
    }
}

scenario "null object is checked", {
    when "a null object is checked", {
        isValid = constraint.validateValue(null)
    }
    then "it should be considered valid", {
        isValid.shouldBe true
    }
}

scenario "object of unexpected type is checked", {
    when "an String is checked", {
        isValid = constraint.validateValue("not valid")
    }
    then "it should be considered invalid", {
        isValid.shouldBe false
    }
}

scenario "in range value is checked", {
    when "5 is checked", {
        isValid = constraint.validate(5)
    }
    then "it should be considered valid", {
        isValid.shouldBe true
    }
}

scenario "min value is checked", {
    when "1 is checked", {
        isValid = constraint.validate(1)
    }
    then "it should be considered valid", {
        isValid.shouldBe true
    }
}

scenario "max value is checked", {
    when "10 is checked", {
        isValid = constraint.validate(10)
    }
    then "it should be considered valid", {
        isValid.shouldBe true
    }
}

scenario "lower than min value is checked", {
    when "0 is checked", {
        isValid = constraint.validate(0)
    }
    then "it should be considered invalid", {
        isValid.shouldBe false
    }
}

scenario "greater than max value is checked", {
    when "100 is checked", {
        isValid = constraint.validate(100)
    }
    then "it should be considered invalid", {
        isValid.shouldBe false
    }
}

scenario "create a between constraint with min == max", {
    given "a min allowed value equal to max allowed one", {
        minValue = 10
        maxValue = 10
    }
    when "try to create between constraint with them", {
        createConstraint = { -> new BetweenConstraint<Integer>(minValue, maxValue) }
    }
    then "IllegalArgumentException should be throwed", {
        ensureThrows(IllegalArgumentException) {
            createConstraint()
        }
    }
}

scenario "create a between constraint with min > max", {
    given "a min allowed value greater than max allowed one", {
        minValue = 10
        maxValue = 1
    }
    when "try to create between constraint with them", {
        constraint = new BetweenConstraint<Integer>(minValue, maxValue)
    }
    then "min and max should be inverted", {
        constraint.min.shouldBe maxValue
        constraint.max.shouldBe minValue
    }
}
